
package util.www.jsp;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JspUtil {
    
    
    //To disable browser cache for JSP pages 
    //http://stackoverflow.com/questions/11337785/how-to-clear-browser-cache-programmatically-in-jsp
    //http://stackoverflow.com/questions/3055268/add-an-expires-or-a-cache-control-header-in-jsp
    //VBScript: <%response.Expires=-1%>  
    //USO: Si no, el navegador guardará un caché de la primera version de una pagina web
    //Entonces por más que se actualice la pagina en el servidor, el cliente seguirá leyendo la version del caché
    //ALTERNATIVA: Borrar el historial con el navegador
    public static void deshabilitarCache(HttpServletResponse httpResponse) {
        httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
        httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
        httpResponse.setDateHeader("Expires", 0); // Proxies.
    }
    
    //http://stackoverflow.com/questions/12958759/getting-ip-address-of-client-in-jsp-page
    public static String getDireccionIPClienteProxy(HttpServletRequest request) {
        return request.getHeader("X-FORWARDED-FOR");//IPaddress
    }
    
    //http://stackoverflow.com/questions/12958759/getting-ip-address-of-client-in-jsp-page
    public static String getPuertaEnlaceClienteProxy(HttpServletRequest request) {
        return request.getHeader("VIA"); //Gateway 
    }    
   
    //Get client IP address from JSP
    //http://www.rgagnon.com/javadetails/java-0363.html
    public static String getDireccionIPCliente(HttpServletRequest request) {   
        return request.getRemoteAddr();//request.getRemoteHost();
    }
    
    public static String getNavegadorWeb(HttpServletRequest request) {
        return request.getHeader("User-Agent");
    }
    
    //http://mghtb.com/detecting-browser-type-on-the-server-in-java-jsp/
    public static void tipoNavegadorWeb(HttpServletRequest request) {
        Integer minimumFirefox = 12;
        Integer minimumSafari = 51; /* This number is 10 times the actual version number */
        Integer minimumChrome = 18;
        Integer minimumIE = 9;

        String ua = request.getHeader("User-Agent");

        boolean isMSIE = (ua != null && ua.indexOf("MSIE") != -1);
        boolean isMSIE10 = (ua != null && ua.indexOf("MSIE 10") != -1);
        boolean isMSIE9 = (ua != null && ua.indexOf("MSIE 9") != -1);
        boolean isMSIE8 = (ua != null && ua.indexOf("MSIE 8") != -1);
        boolean isChrome = (ua != null && ua.indexOf("Chrome/") != -1);
        boolean isSafari = (!isChrome && (ua != null && ua.indexOf("Safari/") != -1));
        boolean isSafari5 = (!isChrome && (ua != null && ua.indexOf("Safari/") != -1 && ua.indexOf("Version/5.") != -1));
        boolean isFirefox = (ua != null && ua.indexOf("Firefox/") != -1);

        String version = "1";
        if (isFirefox) {
            // Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0
            version = ua.replaceAll("^.*?Firefox/", "").replaceAll("\\.\\d+", "");
        } else if (isChrome) {
            // Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5
            version = ua.replaceAll("^.*?Chrome/(\\d+)\\..*$", "$1");
        } else if (isSafari) {
            // Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
            version = ua.replaceAll("^.*?Version/(\\d+)\\.(\\d+).*$", "$1$2");
        } else if (isMSIE) {
            // Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)
            version = ua.replaceAll("^.*?MSIE\\s+(\\d+).*$", "$1");
        }
        boolean unsupportedBrowser = (isFirefox && Integer.parseInt(version) < minimumFirefox)
        || (isChrome && Integer.parseInt(version) < minimumChrome)
        || (isSafari && Integer.parseInt(version) < minimumSafari)
        || (isMSIE && Integer.parseInt(version) < minimumIE);
    }
    
    public static String getParametros(HttpServletRequest request) {
        return getParametros(request,null);
    }

    public static String getParametros(HttpServletRequest request, String[] exceptoParametros) {
        StringBuilder sb = new StringBuilder();
        Map<Object,Object> map = request.getParameterMap();
        
        if(exceptoParametros==null) {
            for(Map.Entry<Object,Object> e : map.entrySet()) { 
                String key = (String) e.getKey();
                sb.append(key);
                sb.append("=");
                sb.append(request.getParameter(key));
                sb.append("&");
            }
        }
        else {
            for(Map.Entry<Object,Object> e : map.entrySet()) {          
                String key = (String) e.getKey();

                boolean b = false;
                for(String par:exceptoParametros) {
                    if(key.equals(par)) {
                        b = true;
                        break;
                    }
                }            
                if(b)
                    continue;

                sb.append(key);
                sb.append("=");
                sb.append(request.getParameter(key));
                sb.append("&");
            }
        }
        
          
        
        if(sb.length()>0)
            sb.deleteCharAt(sb.length()-1);//borrando ultimo &
        return sb.toString();
    }
}
